let _token = $('input[name=_token]').eq(0).val()
let uploadedImageType = 'image/jpeg'
let $image = $('#image')
  // 项目信息添加
  ; (function ($, global) {
    let $form = $('#project-form')
    let $submit = $form.find('button.submit')
    let verify = {}

    let fnInitAddReq = function () {
      let data = new FormData($form[0])
      data.delete('img')
      let imageData
      try {
        imageData = $image.cropper('getCroppedCanvas').toBlob(function (blob) {
          data.append('pic', blob)
          req = data
        }, uploadedImageType, 1)
      } catch (e) {
        req = data
      }
      return req
    }

    let fnAddAjax = function (data) {
      fnUploadAjax('POST', 'project/add', data, 'json', fnCommonAddSuccess, fnCommonAddError)
    }

    let fnTrigger = function () {
      $submit.click(function () {
        let data = new FormData($form[0])
        data.delete('img')
        try {
          imageData = $image.cropper('getCroppedCanvas').toBlob(function (blob) {
            data.append('pic', blob)
            let objData = {}
            data.forEach((value, key) => {
              objData[key] = value;
            })
            if (reqVerify(objData, verify)) {
              fnLoading()
              fnAddAjax(data)
            }
          }, uploadedImageType)
        } catch (e) {
          layer.msg('请选择图片')
        }
      })
    }

    fnTrigger()
  }(jQuery, window))

  // 项目信息修改
  ; (function ($, global) {
    let $form = $('#project-edit-form')
    let $submit = $form.find('button.submit')
    let verify = {}

    let fnInitAddReq = function () {
      return new FormData($form[0])
    }

    let fnEditAjax = function (data) {
      fnUploadAjax('POST', 'project/edit?id=' + data.get('id'), data, 'json', fnCommonEditSuccess, fnCommonEditError)
    }

    let fnTrigger = function () {
      $submit.click(function () {
        let data = fnInitAddReq();
        data.delete('img')
        try {
          imageData = $image.cropper('getCroppedCanvas').toBlob(function (blob) {
            data.append('pic', blob)
            let objData = {}
            data.forEach((value, key) => {
              objData[key] = value;
            })
            if (reqVerify(objData, verify)) {
              fnLoading()
              fnEditAjax(data)
            }
          }, uploadedImageType)
        } catch (e) {
          console.log(e)
          let req = fnInitAddReq()
          if (reqVerify(req, verify)) {
            fnLoading()
            fnEditAjax(req)
          }
        }
      })
    }

    fnTrigger()
  }(jQuery, window))

  ; (function ($, global) { // tr的增减
    let $commonTable = $('#common-table')
    let $errorTable = $('#error-table')
    let $historyTable = $('#history-table')
    let fnAppendCommonTr = function () {
      let $tr = $(commonTr)
      $commonTable.find('tbody').append($tr)
    }

    let fnAppendErrorTr = function () {
      let $tr = $(errorTr)
      $errorTable.find('tbody').append($tr)
    }

    let fnAppendHistoryTr = function () {
      let $tr = $(historyTr)
      $historyTable.find('tbody').append($tr)
    }

    let fnRemoveTr = function (e) {
      $(e).parents('tr').remove()
    }
    global.fnAppendCommonTr = fnAppendCommonTr
    global.fnAppendErrorTr = fnAppendErrorTr
    global.fnAppendHistoryTr = fnAppendHistoryTr
    global.fnRemoveTr = fnRemoveTr
  })(jQuery, window)

  // 删除公共参数
  ; (function ($, global) {
    let $span = $('.del-common-span')

    let fnInitReq = function ($del) {
      return {
        id: $del.attr('data-id'),
        _token: _token
      }
    }

    let fnAjax = function (data) {
      fnApiAjax('POST', 'commonparam/del?id=' + data.id, data, 'json', fnSuccess, fnError)
    }

    let fnSuccess = function (data) {
      layer.msg('删除成功', { icon: 1 })
      $('#commonparam-' + data.id).fadeTo('slow', 0.01, function () {
        $(this).slideUp('slow', function () {
          $(this).remove()
        })
      })
    }

    let fnError = function () { }

    let fnTrigger = function () {
      $span.click(function () {
        let req = fnInitReq($(this))
        fnDelConfirm(req)
      })
    }

    let fnDelConfirm = function (data) {
      layer.confirm('确定删除此项？', {
        btn: ['确定', '取消'] // 按钮
      }, function () {
        fnLoading()
        fnAjax(data)
      }, function () { })
    }
    fnTrigger()
  }(jQuery, window))

  // 删除错误码
  ; (function ($, global) {
    let $span = $('.del-error-span')

    let fnInitReq = function ($del) {
      return {
        id: $del.attr('data-id'),
        _token: _token
      }
    }

    let fnAjax = function (data) {
      fnApiAjax('POST', 'errorcode/del?id=' + data.id, data, 'json', fnSuccess, fnError)
    }

    let fnSuccess = function (data) {
      layer.msg('删除成功', { icon: 1 })
      $('#errorcode-' + data.id).fadeTo('slow', 0.01, function () {
        $(this).slideUp('slow', function () {
          $(this).remove()
        })
      })
    }

    let fnError = function () { }

    let fnTrigger = function () {
      $span.click(function () {
        let req = fnInitReq($(this))
        fnDelConfirm(req)
      })
    }

    let fnDelConfirm = function (data) {
      layer.confirm('确定删除此项？', {
        btn: ['确定', '取消'] // 按钮
      }, function () {
        fnLoading()
        fnAjax(data)
      }, function () { })
    }
    fnTrigger()
  }(jQuery, window))

  ; (function ($, global) {
    let _token = $('input[name=_token]').eq(0).val()

    let req = new FormData()
    let URL = global.URL || global.webkitURL
    let Cropper = global.Cropper
    let $form = $('.project-form')

    let $file = $form.find('input[name=img]')
    let $cropperGroup = $form.find('.form-cropper')
    let $cropperControl = $form.find('.form-cropper-control')
    let $cropperPreview = $form.find('.form-preview')
    let $image = $('#image')
    let originalImageURL = $image.attr('src')
    let uploadedImageType = 'image/jpeg'
    let uploadedImageURL
    let $crop
    let minContainerWidth = 538

    let $deleteSpan = $('.slider-delete-span')
    let $addSpan = $('.slider-add-span')
    let options = {
      viewMode: 2,
      preview: '.img-preview',
      aspectRatio: 7 / 3,
      minContainerWidth: 538,
      maxContainerWidth: 538,
      minContainerHeight: 1000,
      crop: function (e) { },
      ready: function () {
        fnStopLoading()
        $cropperGroup.show()
        $cropperControl.show()
        $cropperPreview.show()
      }
    }

    let fnTrigger = function () {
      $file.change(function () {
        var files = this.files
        var file
        if (files && files.length) {
          file = files[0]
          if (/^image\/\w+$/.test(file.type)) {
            uploadedImageType = file.type
            if (uploadedImageURL) {
              URL.revokeObjectURL(uploadedImageURL)
            }
            uploadedImageURL = URL.createObjectURL(file)
            $image.cropper('destroy').attr('src', uploadedImageURL)
            $image[0].onload = function () {
              let width = this.width
              let height = this.height
              options.minContainerHeight = options.minContainerWidth * height / width
              $image.cropper(options)
            }
            global.cropimg = $image
            fnCropScaleInit()
            $file.val('')
          } else {
            layer.msg('Please choose an image file.')
          }
        }
      })
    }

    let fnImageClear = function () {
      if ($image) {
        $image.cropper('destroy').hide()
      }
    }

    let fnImageCropper = function () {
      $crop = $form.find('input[name=crop]')
      $image = $('#image')
      global.cropimg = $image
      $image.cropper(options)
    }

    let fnCropScaleInit = function () {
      global.scaleX = 1
      global.scaleY = 1
    }

    fnTrigger()
  })(jQuery, window)

  //项目历史记录更改
  ; (function ($, global) {
    let $form = $('#project-history-form')
    let $submit = $form.find('button.submit')

    let fnInitEditReq = function () {
      return new FormData($form[0])
    }

    let fnEditAjax = function (data) {
      data.append('_token', _token)
      fnUploadAjax('POST', 'projecthistory/edit?project_id=' + data.get('project_id'), data, 'json', fnCommonEditSuccess, fnCommonEditError)
    }

    let fnTrigger = function () {
      $submit.click(function() {
        let data = fnInitEditReq();
        fnLoading()
        fnEditAjax(data);
      })
    }

    fnTrigger()
  })(jQuery, window)

  //删除项目历史记录
  ;(function($, global) {
    let $span = $('.del-history-span')

    let fnInitReq = function ($del) {
      return {
        id: $del.attr('data-id'),
        _token: _token
      }
    }

    let fnAjax = function (data) {
      fnApiAjax('POST', 'projecthistory/del?id=' + data.id, data, 'json', fnSuccess, fnError)
    }

    let fnSuccess = function (data) {
      layer.msg('删除成功', { icon: 1 })
      $('#history-' + data.id).fadeTo('slow', 0.01, function () {
        $(this).slideUp('slow', function () {
          $(this).remove()
        })
      })
    }

    let fnError = function () { }

    let fnTrigger = function () {
      $span.click(function () {
        let req = fnInitReq($(this))
        fnDelConfirm(req)
      })
    }

    let fnDelConfirm = function (data) {
      layer.confirm('确定删除此项？', {
        btn: ['确定', '取消'] // 按钮
      }, function () {
        fnLoading()
        fnAjax(data)
      }, function () { })
    }
    fnTrigger()
  })(jQuery, window)